//
//  FlatteningOfLayerViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/11.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "FlatteningOfLayerViewController.h"

@interface FlatteningOfLayerViewController ()

@property (nonatomic, strong) UIView *superView; // 父视图
@property (nonatomic, strong) UIView *subView; // 子视图
@property (nonatomic, strong) UIView *backGroundView;

@end

@implementation FlatteningOfLayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.backGroundView = [[UIView alloc] initWithFrame:self.view.frame];
    self.backGroundView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_backGroundView];
    
    self.superView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    self.superView.backgroundColor = [UIColor whiteColor];
    [self.backGroundView addSubview:_superView];
    
    self.subView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
    self.subView.backgroundColor = [UIColor redColor];
    [self.superView addSubview:_subView];
    
    UIButton *zRorateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    zRorateButton.frame = CGRectMake(0, 400, 200, 30);
    [zRorateButton setTitle:@"Z轴双向旋转" forState:UIControlStateNormal];
    [zRorateButton addTarget:self action:@selector(zRotate:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:zRorateButton];
    
    UIButton *yRorateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    yRorateButton.frame = CGRectMake(0, 450, 200, 30);
    [yRorateButton setTitle:@"y轴双向旋转" forState:UIControlStateNormal];
    [yRorateButton addTarget:self action:@selector(yRotate:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:yRorateButton];
    
    UIButton *recoverButton = [UIButton buttonWithType:UIButtonTypeCustom];
    recoverButton.frame = CGRectMake(250, 400, 100, 30);
    [recoverButton setTitle:@"回复原状" forState:UIControlStateNormal];
    [recoverButton addTarget:self action:@selector(recover:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:recoverButton];
}

// Z轴的双向旋转
- (void)zRotate:(UIButton *)button
{
    // 结论：注意做了-45度旋转的内部图层是怎样抵消旋转45度的图层，从而恢复正常状态的。如果内部图层相对外部图层做了相反的变换（这里是绕Z轴的旋转），那么按照逻辑这两个变换将被相互抵消。即subView的旋转方向不会改变
    // 父视图正向旋转45度
    self.superView.layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);
    // 子视图反向旋转45度
    self.subView.layer.transform = CATransform3DMakeRotation(-M_PI_4, 0, 0, 1);
    
}

// Y轴的双向旋转
- (void)yRotate:(UIButton *)button
{
    // 我们期望的是子视图和父视图的Y轴相互抵消，即子视图还是正面朝向我们，但是真实情况不是这样
#warning 真实的情况 subview图层仍然向左侧旋转，并且发生了扭曲，即好像缩短了一样，原因：这是由于尽管Core Animation图层存在于3D空间之内，但它们并不都存在同一个3D空间。⚠️每个图层的3D场景其实是扁平化的，当你从正面观察一个图层，看到的实际上由子图层创建的想象出来的3D场景，但当你倾斜这个图层，你会发现实际上这个3D场景仅仅是被绘制在图层的表面。
    
    /**
     *类似的，当你在玩一个3D游戏，实际上仅仅是把屏幕做了一次倾斜，或许在游戏中可以看见有一面墙在你面前，但是倾斜屏幕并不能够看见墙里面的东西。⚠️所有场景里面绘制的东西并不会随着你观察它的角度改变而发生变化；图层也是同样的道理。
       这使得用Core Animation创建非常复杂的3D场景变得十分困难。你不能够使用图层树去创建一个3D结构的层级关系--在相同场景下的任何3D表面必须和同样的图层保持一致，这是因为每个的父视图都把它的子视图扁平化了。
     但是CALayer有一个叫做CATransformLayer的子类来解决这个问题，这个在第五章的deme里的有介绍
     */
    
    
    // 父视图绕Y轴正向旋转45度
    CATransform3D transform3DOne = CATransform3DIdentity;
    transform3DOne.m34 = - 1.0 / 500.0;
    transform3DOne = CATransform3DRotate(transform3DOne, M_PI_4, 0, 1, 0);
    self.superView.layer.transform = transform3DOne;
    
    // 子视图绕Y轴反向旋转45度
    CATransform3D transform3DTwo = CATransform3DIdentity;
    transform3DTwo.m34 = - 1.0 / 500.0;
    transform3DTwo = CATransform3DRotate(transform3DTwo, -M_PI_4, 0, 1, 0);
    self.subView.layer.transform = transform3DTwo;
}

// 回复原状
- (void)recover:(UIButton *)button
{
    self.superView.layer.transform = CATransform3DIdentity;
    self.subView.layer.transform = CATransform3DIdentity;
}

@end
